import common from './common.js'
import OnBluetoothAdapterStateChangeResult = UniApp.OnBluetoothAdapterStateChangeResult;
import OnBluetoothDeviceFoundResult = UniApp.OnBluetoothDeviceFoundResult;

class BluetoothUtils {

	public openBluetoothAdapter(callback: Function) {
		let errCode = '';
		return new Promise((resolve, reject) => {
			uni.openBluetoothAdapter({
				success: (res: any) => {
					console.log(res)
					resolve(res);
					callback(common.getErrorCode(0));
				},
				fail: err => {
					reject(err);
					errCode = err.errCode;
					callback(common.getErrorCode(errCode));
				},
			});
		});
	}

	public getBluetoothAdapterState(callback: Function) {
		uni.getBluetoothAdapterState({
			success(res) {
				console.log('getBluetoothAdapterState res')
				console.log(res)
				const obj = common.stringToJson(res)
				callback(obj)
			}
		})
	}

	public onBluetoothAdapterStateChange(callback: Function) {
		uni.onBluetoothAdapterStateChange((res: OnBluetoothAdapterStateChangeResult) => {
			if (callback) {
				const obj = common.stringToJson(res)
				callback(obj)
			}
		})
	}

	public onBluetoothDeviceFound(callback: Function) {
		uni.onBluetoothDeviceFound((devices: OnBluetoothDeviceFoundResult) => {
			if (typeof (callback) === 'function') {
				if (typeof (devices) === 'string') {
					devices = common.stringToJson(devices);
				}
				callback(devices)
			}
		})
	}

	public getBluetoothDevices(callback: Function) {
		uni.getBluetoothDevices({
			success: result => {
				callback(result);
			}
		})
	}

	public startBluetoothDevicesDiscovery(callback: Function, serviceIdList?: string[]) {
		let _self = this;
		uni.startBluetoothDevicesDiscovery({
			services: serviceIdList,
			success() {
				console.log("startBluetoothDevicesDiscovery success")
				_self.onBluetoothDeviceFound(callback)
			},
			fail(res: any) {
				console.log("startBluetoothDevicesDiscovery fail")
				console.log(res)
				if (callback) {
					callback && callback(res.errCode)
				}
			}
		})
	}

	public stopBluetoothDevicesDiscovery(callback: Function) {
		uni.stopBluetoothDevicesDiscovery({
			success(res) {
				if (callback) {
					callback && callback(0)
				}
				console.log(res)
			}, fail(res) {
				if (callback) {
					callback && callback(res.errCode)
				}
			}
		})
	}

	public onBLEConnectionStateChange(callback: Function) {
		uni.onBLEConnectionStateChange(function (res) {
			if (callback) {
				const obj = common.stringToJson(res)
				callback(obj)
			}
		})
	}

	public getBLEDeviceServices(deviceId: string, callback: Function) {
		uni.getBLEDeviceServices({
			// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
			deviceId,
			success(res) {
				console.log('device services:', res.services)
				callback && callback(common.getErrorCode(0))
			},
			fail(res) {
				callback && callback(res.errCode)
			}
		})
	}

	public createBLEConnection(deviceId: string, callback: Function) {
		uni.createBLEConnection({
			// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
			deviceId,
			success(res) {
				callback && callback(common.getErrorCode(0))
			},
			fail(res) {
				callback && callback(common.getErrorCode(res.errCode))
			}
		})
	}

	/** 通知notify--低功耗蓝牙设备的特征值的值 **/
	public notifyBLECharacteristicValueChange(deviceId: string, serviceId: string, characteristicId: string, state = true, callback: Function) {
		uni.notifyBLECharacteristicValueChange({
			state, // 启用 notify 功能
			// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
			deviceId,
			// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
			serviceId,
			// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
			characteristicId,
			success(res) {
				return callback(common.getErrorCode(0)) && (common.getErrorCode(0))
			},
			fail(res) {
				return callback(common.getErrorCode(res.errCode)) && (common.getErrorCode(res.errCode));
			}
		})
	}

	public onBLECharacteristicValueChange(callback: Function) {
		uni.onBLECharacteristicValueChange(function (res) {
			if (callback) {
				const obj = common.stringToJson(res)
				callback(obj)
			}
		})
	}

	public writeBLECharacteristicValue(deviceId: string, serviceId: string, characteristicId: string, data: ArrayBuffer[]) {
		uni.writeBLECharacteristicValue({
			deviceId: deviceId,
			serviceId: serviceId,
			characteristicId: characteristicId,
			value: data,
			success(res) {
				console.log('message发送成功', JSON.stringify(res));
				// resolve(res);
			},
			fail(err) {
				console.log('message发送失败', JSON.stringify(err));
				// reject(err);
			}
		});
	}
}

export default new BluetoothUtils();
